package com.itzhihao.controller;

import com.itzhihao.entity.Address;
import com.itzhihao.service.IAddressService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {

    @Resource
    private IAddressService addressService;

    @PostMapping("/list")
    public TableDataInfo getList(@RequestBody PageDomain pageDomain){
        return addressService.getList(pageDomain);
    }

    @GetMapping("/getDefault")
    public AjaxResult getDefault(){
        Address address = addressService.getDefault();
        return success(address);
    }

    @PutMapping("/setDefault")
    public AjaxResult setResult(@RequestBody Address address){
        Boolean b = addressService.setDefault(address);
        if(b){
            return success();
        }
        return error();
    }

    /**
     * 查看地址详情
     * @param id
     * @return
     */
    @GetMapping
    public AjaxResult getDetail(@RequestParam(value = "address_id") Long id){

        Address address =  addressService.getDetail(id);
        return success(address);
    }

    @PutMapping
    public AjaxResult update(@RequestBody Address address){
        boolean isUpdate = addressService.updateOrSave(address);
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @DeleteMapping("/delete/{address_id}")
    public AjaxResult delete(@PathVariable(value = "address_id") Long id){
        boolean isDelete = addressService.deleteById(id);
        if(isDelete){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败，请重试");
        }
    }

    /**
     * 新增
     * @param address
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody Address address){
        System.out.println(address);
        boolean isSave = addressService.updateOrSave(address);
        if (isSave){
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Delete("/delete/{ids}")
    public AjaxResult deleteBatch(@PathVariable String ids){
        Long[] idLongArr = Convert.toLongArray(ids);
        List<Long> idLongList = Arrays.asList(idLongArr);
        boolean isDelete = addressService.deleteByIds(idLongList);
        if(isDelete){
            return success("批量删除成功");
        }else{
            return error("批量删除失败");
        }
    }

//    @GetMapping("/addAddrForUser")
//    public AjaxResult addAddr(@RequestBody SysUser sysUser){
//        userService.addAddr(sysUser);
//        return success();
//    }

}
